package com.myFwk.config;

import com.myFwk.constant.Constants;
import lombok.extern.slf4j.Slf4j;
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Title:
 * @Description:
 * @Author: guowl
 * @version： 1.0
 * @Date:2021/10/19
 * @Copyright: Copyright(c)2021 RedaFlight.com All Rights Reserved
 */
@Configuration
@Slf4j
public class RabbitMqConfig {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 声明交换机
     * 一个交换机可以绑定多个队列
     */
    @Bean("FwkExchange")
    public Exchange directExchange() {
        Exchange exchange = ExchangeBuilder.directExchange(Constants.EXCHANGE_FWK).durable(true).build();
        return exchange;
    }

    /**
     * 声明交换机
     * 一个交换机可以绑定多个队列
     */
    @Bean("DeadFwkExchange")
    public Exchange DeadFwkExchange() {
        Exchange exchange = ExchangeBuilder.directExchange(Constants.DEAD_EXCHANGE_FWK).durable(true).build();
        return exchange;
    }

    @Bean
    public Queue queue_0() {

        return new Queue(Constants.QUEUE_FWK_0, true);
    }

    @Bean
    public Queue queue_1() {
        Map<String, Object> args = new HashMap<>();
        /**
         * 设置死信队列 设置邦定死信队列
         */
        args.put("DEAD_EXCHANGE_FWK", Constants.DEAD_EXCHANGE_FWK);
        args.put("DELAY_ROUTING_KEY_0", Constants.DELAY_ROUTING_KEY_0);
        return QueueBuilder.durable(Constants.QUEUE_FWK_1).withArguments(args).build();
    }

    @Bean
    public Queue queue_2() {
        return new Queue(Constants.QUEUE_FWK_2, true);
    }

    @Bean
    public Queue queue_3() {
        return new Queue(Constants.QUEUE_FWK_3, true);
    }

    @Bean
    public Queue delay_queue_1() {
        return new Queue(Constants.DELAY_QUEUE_FWK_0, true);
    }

    /**
     *
     */
    @Bean
    public Binding binding_0() {

        return BindingBuilder.bind(queue_0()).to(directExchange()).with(Constants.ROUTING_KEY_0).noargs();
    }

    @Bean
    public Binding binding_1() {
        return BindingBuilder.bind(queue_1()).to(directExchange()).with(Constants.ROUTING_KEY_1).noargs();
    }

    @Bean
    public Binding binding_2() {
        return BindingBuilder.bind(queue_2()).to(directExchange()).with(Constants.ROUTING_KEY_2).noargs();
    }

    @Bean
    public Binding binding_3() {
        return BindingBuilder.bind(queue_3()).to(directExchange()).with(Constants.ROUTING_KEY_3).noargs();
    }

    @Bean
    public Binding binding_4() {
        return BindingBuilder.bind(delay_queue_1()).to(DeadFwkExchange()).with(Constants.DELAY_ROUTING_KEY_0).noargs();
    }

    /**
     * 主要确保消息能够成功的发送到交换机
     */
//    @PostConstruct
//    public void initRabbitTemplate() {
//        /**
//         * 设置开启回调函数
//         */
//        rabbitTemplate.setExchange(Constants.EXCHANGE_FWK);
//        rabbitTemplate.setMandatory(true);
//        //设置发送消息确认回调机制
//        //此确认机制是当消息到达交换机的时候
//        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
//            @Override
//            public void confirm(CorrelationData correlationData, boolean b, String s) {
//                log.info("ConfirmCallback ID:{},内容:{}", correlationData, b, s);
//            }
//        });
////        /**
////         * 这个主要是当消费者消费之后的一个回调函数
////         *  TODO 这个回调函数未起作用 需要核实一下 是什么问题。
////         */
////        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
////            @Override
////            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
////                System.out.println("===============================================");
////                log.info("ReturnCallback 内容：{}", message);
////                log.info("ReturnCallback 回应码：{}", i);
////                log.info("ReturnCallback 回应信息：{}", s);
////                log.info("ReturnCallback 交换机：{}", s1);
////                log.info("ReturnCallback 路由键：{}", s2);
////            }
////        });
//    }
//

}
